/**
 * OSAmI: Open Source Ambient Intelligence
 * A simple discovery protocol (based on multicast communications) for constrained devices.
 * Copyright (C) Lionel Debroux 2006 (as a TER/Magistere M1 student in the LSR-IMAG-ADELE team, for a demo in the H-Omega project)
 * Copyright (C) Lionel Debroux 2010 (as an engineer in the LCIS-INPG-CTSYS team, for a pre-demo in the FR part of the OSAMI project)
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
 */

package fr.imag.adele.homega.impl.unidiscovery;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import java.util.logging.Logger;

/**
 * Helper class for encapsulating device metadata representation and handling. It also defines several constant strings used by
 * the other classes of the same package. Having those defined in a single place makes it easier to change the format. 
 */
/*package*/ class DeviceMetadata {

	/**
	 * Separator between key and value in a key-value pair.
	 */
	public static final String DEVICE_METADATA_KV_SEPARATOR = "|";

	/**
	 * Separator between key-value pairs.
	 */
	public static final String DEVICE_METADATA_FIELD_SEPARATOR = ";";


	private HashMap<String,String> metadata;
	private String id;


	public DeviceMetadata(String id) {
		metadata = new HashMap<String,String>();
		this.id = id;
	}

	public void addMetadata(String property, String value) throws Exception {
		//Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info("Adding metadata \"" + property + "|" + value + "\"");
		if (   property.indexOf(DEVICE_METADATA_KV_SEPARATOR) != -1
			|| property.indexOf(0) != -1
			|| value.indexOf(DEVICE_METADATA_KV_SEPARATOR) != -1
			|| value.indexOf(0) != -1) {
			throw new Exception("Metadata contains \"" + DEVICE_METADATA_KV_SEPARATOR + "\" or null characters, skipping !");
		}
		else {
			metadata.put(property, value);
		}
	}

	public void removeMetadata(String property) {
		//Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info("Removing metadata for \"" + property + "\"");
		metadata.remove(property);
	}

	public String getMetadata(String property) {
		String value = null;
		//Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info("Getting metadata for \"" + property + "\"");
		value = metadata.get(property);

		return value;
	}

	public String toString() {
		String str = new String(id + Device.DEVICE_FIELD_SEPARATOR);
		Set<Entry<String,String> > set = metadata.entrySet();
		Iterator<Entry<String,String> > i = set.iterator();
		Entry<String,String> e;

		while (i.hasNext()) {
			e = i.next();
			str = str + e.getKey() + DEVICE_METADATA_KV_SEPARATOR + e.getValue() + DEVICE_METADATA_FIELD_SEPARATOR;
		}

		return str + Device.DEVICE_FIELD_SEPARATOR;
	}

	/*public byte [] toBytes() {
		return this.toString().getBytes();
	}*/
}
